var ContextMenu = {
  addListeners: function()
  {
    $("main-popup").addEventListener("popupshowing", ContextMenu.popupShowing, false);
  },
  
  removeListeners: function()
  {
    $("main-popup").removeEventListener("popupshowing", ContextMenu.popupShowing, false);
  },
  
  popupShowing: function(aEvent) {
    var cut = $("cut-menuitem");
    var copy = $("copy-menuitem");
    var paste = $("paste-menuitem");
    var del = $("delete-menuitem");

    var isContentSelected = !document.commandDispatcher.focusedWindow.getSelection().isCollapsed;

    var target = document.popupNode;
    var isTextField = target instanceof HTMLTextAreaElement;
    if (target instanceof HTMLInputElement && (target.type == "text" || target.type == "password"))
      isTextField = true;
    var isTextSelectied= (isTextField && target.selectionStart != target.selectionEnd);

    cut.setAttribute("disabled", ((!isTextField || !isTextSelectied) ? "true" : "false"));
    copy.setAttribute("disabled", (((!isTextField || !isTextSelectied) && !isContentSelected) ? "true" : "false"));
    paste.setAttribute("disabled", (!isTextField ? "true" : "false"));
    del.setAttribute("disabled", (!isTextField ? "true" : "false"));
  }
}